package com.license.logic.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.license.common.util.Constants;
import com.license.common.util.DbConnection;
import com.license.logic.alimentation.Food;
import com.license.logic.alimentation.FoodCharacteristics;
import com.license.logic.behaviorlog.WriteXML;

public class NutrionDAO {
	private Connection connection;

	public NutrionDAO() {
		this.connection = DbConnection.getCon();
	}

	public Map<String, List<Food>> getAllFoods() {
		Map<String, List<Food>> foodMap = new HashMap<String, List<Food>>();
		foodMap.put(Constants.BREAKFAST, new ArrayList<Food>());
		foodMap.put(Constants.LUNCH, new ArrayList<Food>());
		foodMap.put(Constants.DINNER, new ArrayList<Food>());
		foodMap.put(Constants.SNACK, new ArrayList<Food>());

		Statement stmt;
		try {
			stmt = this.connection.createStatement();
			ResultSet rs;
			rs = stmt
					.executeQuery("SELECT sfi.name, sfi.gramsPerPortion, sfi.type, nd.nutr_val" 
							+ " FROM sr24.nutrient_data nd inner join selectedfooditem  sfi"
							+ " on nd.ndb_no = sfi.ndb_no"
							+ " inner join sr24.nutrient_definition def"
							+ " on nd.nutr_no = def.nutr_no"
							+ " where def.tagname = 'ENERC_KCAL';");
			while (rs.next()) {
				Food food = new Food();
				food.setName(rs.getString("name"));
				FoodCharacteristics characteristics = new FoodCharacteristics();
				characteristics.setKalory(rs.getInt("nutr_val"));
				characteristics
						.setGramsPerPortion(rs.getInt("gramsPerPortion"));
				food.setCharacteristics(characteristics);
				for (String type : rs.getString("type").split(",")) {
					foodMap.get(type).add(food);
				}
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return foodMap;
	}

	public List<String> getAllActivityNames() {
		List<String> activities = new ArrayList<String>();
		Statement stmt;
		try {
			stmt = this.connection.createStatement();
			ResultSet rs;
			rs = stmt.executeQuery("Select * from activities");
			while (rs.next()) {
				activities.add(rs.getString("exercise"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return activities;
	}

	public Map<String, Set<String>> getExceptedFoods() {
		WriteXML writeXML = new WriteXML();
		return writeXML.getFoodExcepted(Constants.FOOD_RULES_PATH + "foodRules.xml");
	}
	
	public Map<String, String> getClasifiedFoodTypeMap(){
		Map<String, String> foodMap = new HashMap<String, String>();
		
		Statement stmt;
		try {
			stmt = this.connection.createStatement();
			ResultSet rs;
			rs = stmt
					.executeQuery("SELECT sfi.name, sfi.gramsPerPortion, sfi.foodType, nd.nutr_val" 
							+ " FROM sr24.nutrient_data nd inner join selectedfooditem  sfi"
							+ " on nd.ndb_no = sfi.ndb_no"
							+ " inner join sr24.nutrient_definition def"
							+ " on nd.nutr_no = def.nutr_no"
							+ " where def.tagname = 'ENERC_KCAL';");
			while (rs.next()) {
				foodMap.put(rs.getString("name"),  rs.getString("foodType"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return foodMap;
	}
}
